﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.Extensions.Options;
using OASystem.BLL.Service;
using OASystem.Common;
using OASystem.Model;
using OASystem.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace OASystem.Attributes
{
    public class ActionAttribute : ActionFilterAttribute  //行为过滤器
    {

        ///// <summary>
        ///// 调用控制器对应的Action方法之后的操作
        ///// </summary>
        ///// <param name="filterContext"></param>
        //public override void OnActionExecuted(ActionExecutedContext filterContext)
        //{
        //    Journal systemLogs = new Journal();
        //    string controller = filterContext.RouteData.Values["Controller"].ToString();
        //    string action = filterContext.RouteData.Values["Action"].ToString();
        //    if (action == "LoginOut")
        //    {
        //        return;
        //    }
        //    if (action == "Login" || action == "SecurityCode")
        //    {
        //        //获得IP

        //        string ip = LocalIP.GetLocalIP();
        //        //获得值
        //        systemLogs.Controller = controller;
        //        systemLogs.Action = action;
        //        systemLogs.Time = DateTime.Now;
        //        systemLogs.UserName = ip;
        //        //创建业务层对象
        //        SystemLogsService systemLogsService = new SystemLogsService();
        //        //调用添加方法
        //        systemLogsService.Add(systemLogs);
        //    }
        //    else
        //    {
        //        //获得用户名
        //        string username = AdminContext.context.adminUser.UserName;
        //        //获得值
        //        systemLogs.Controller = controller;
        //        systemLogs.Action = action;
        //        systemLogs.Time = DateTime.Now;
        //        systemLogs.UserName = username;
        //        //创建业务层对象
        //        SystemLogsService systemLogsService = new SystemLogsService();
        //        //调用添加方法
        //        systemLogsService.Add(systemLogs);
        //    }

        //}


        ///// <summary>
        ///// 调用控制器对应的Action方法之前的操作
        ///// </summary>
        ///// <param name="filterContext"></param>
        //public override void OnActionExecuting(ActionExecutingContext filterContext)
        //{
        //    if (AdminContext.context.adminUser != null)
        //    {
        //        //获取用户名
        //        string username = AdminContext.context.adminUser.UserName;
        //        string controller = filterContext.RouteData.Values["Controller"].ToString();
        //        string action = filterContext.RouteData.Values["Action"].ToString();
        //        //获取系统的日志
        //        string msg = $"时间:{DateTime.Now},用户名:{username},正在操作控制器:{controller},页面:{action}";
        //        //获取文件路径
        //        string path = "D:\\Exception.txt";
        //        File.AppendAllText(path, msg);
        //    }
        //}

        ///// <summary>
        ///// 调用控制器对应的Action方法之后页面渲染之后的操作
        ///// </summary>
        ///// <param name="filterContext"></param>
        //public override void OnResultExecuted(ResultExecutedContext filterContext)
        //{

        //}

        ///// <summary>
        ///// 调用控制器对应的Action方法之后页面渲染之前的操作
        ///// </summary>
        ///// <param name="filterContext"></param>
        //public override void OnResultExecuting(ResultExecutingContext filterContext)
        //{

        //}









        //public override void OnActionExecuting(ActionExecutingContext context)
        //{
        //    var year = context.RouteData.Values["year"].ToString();
        //    var month = context.RouteData.Values["month"].ToString();
        //    var slug = context.RouteData.Values["slug"].ToString();
        //    var filePath = BlogRoutes.GetPostFileCachePath(year, month, slug);
        //    //判断文件是否存在 
        //    if (File.Exists(filePath))
        //    {
        //        //获取文件信息对象 
        //        var fileInfo = new FileInfo(filePath);
        //        //结算时间间隔，如果小于等于15分钟，就直接输出 
        //        TimeSpan ts = DateTime.Now - fileInfo.LastWriteTime;
        //        if (ts.TotalMinutes <= 15)
        //        {
        //            //如果存在，直接读取文件 
        //            using (var fs = File.Open(filePath, FileMode.Open))
        //            {
        //                using (var sr = new StreamReader(fs, Encoding.UTF8))
        //                {
        //                    //通过contentresult返回文件内容 
        //                    var contentresult = new ContentResult();
        //                    contentresult.Content = sr.ReadToEnd();
        //                    contentresult.ContentType = "text/html";
        //                    context.Result = contentresult;
        //                }
        //            }
        //        }
        //    }
        //}
        //public override void OnActionExecuted(ActionExecutedContext context)
        //{
        //    //获取结果 
        //    var actionResult = context.Result;
        //    //判断结果是否是一个ViewResult 
        //    if (actionResult is ViewResult)
        //    {
        //        var viewResult = actionResult as ViewResult;
        //        //下面的代码就是执行这个 ViewResult，并把结果的html内容放到一个StringBuiler对象中 
        //        var services = context.HttpContext.RequestServices;
        //        var executor = services.GetRequiredService<IActionResultExecutor<ViewResult>>() as ViewResultExecutor;
        //        var option = services.GetRequiredService<IOptions<MvcViewOptions>>();
        //        var result = executor.FindView(context, viewResult);
        //        result.EnsureSuccessful(originalLocations: null);
        //        var view = result.View;
        //        var builder = new StringBuilder();
        //        using (var writer = new StringWriter(builder))
        //        {
        //            var viewContext = new ViewContext(
        //                context,
        //                view,
        //                viewResult.ViewData,
        //                viewResult.TempData,
        //                writer,
        //                option.Value.HtmlHelperOptions);
        //            view.RenderAsync(viewContext).GetAwaiter().GetResult();
        //            //这句一定要调用，否则内容就会是空的 
        //            writer.Flush();
        //        }
        //        //按照规则生成静态文件名称 
        //        var year = context.RouteData.Values["year"].ToString();
        //        var month = context.RouteData.Values["month"].ToString();
        //        var slug = context.RouteData.Values["slug"].ToString();
        //        var fileDir = BlogRoutes.GetPostFileCacheDirectory(year, month);
        //        if (!Directory.Exists(fileDir))
        //        {
        //            Directory.CreateDirectory(fileDir);
        //        }
        //        var filePath = BlogRoutes.GetPostFileCachePath(year, month, slug);
        //        using (FileStream fs = File.Open(filePath, FileMode.Create))
        //        {
        //            using (StreamWriter sw = new StreamWriter(fs, Encoding.UTF8))
        //            {
        //                sw.Write(builder.ToString());
        //            }
        //        }
        //        //输出当前的结果 
        //        var contentresult = new ContentResult();
        //        contentresult.Content = builder.ToString();
        //        contentresult.ContentType = "text/html";
        //        context.Result = contentresult;
        //    }
        //}
    }
}